package com.sino.business.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.sino.bean.Qxkw;
import com.sino.business.IPatentManageQxkwService;
import com.sino.dao.IPatentManageDAO;
import com.sino.model.QxkwModel;

public class PatentManageQxkwServiceImp implements IPatentManageQxkwService {
	public List getQxkwByName(String hql) {

		String queryHql = "from Qxkw as  qxkw  ";

		List qmList = new ArrayList();

		if (hql.length() > 0) {
			queryHql = queryHql + " where " + hql;
		}
		List qxkwList = this.getIpatentManageDAO().findQxkwByName2(queryHql);

		List qxkwtypeList = this.getIpatentManageDAO().findQxkwType("type");

		Iterator it = qxkwList.iterator();
		while (it.hasNext()) {
			Qxkw qxkw = (Qxkw) it.next();
			QxkwModel qm = new QxkwModel();
			// qm.setId(qxkw.getId());
			Iterator tit = qxkwtypeList.iterator();
			while (tit.hasNext()) {
				Qxkw qk = (Qxkw) tit.next();

				if ("0000".equals(qxkw.getId())) {
					qm.setType("奖励办公室");
				}
				qm.setTypeid(qxkw.getId().substring(0, 2));
				if (qk.getId().equals(qm.getTypeid())) {

					qm.setType(qk.getName());
				}

			}
			qm.setId(qxkw.getId());
			qm.setName(qxkw.getName());
			qmList.add(qm);

		}
		return qmList;

	}

	private IPatentManageDAO ipatentManageDAO;

	public IPatentManageDAO getIpatentManageDAO() {
		return ipatentManageDAO;
	}

	public void setIpatentManageDAO(IPatentManageDAO ipatentManageDAO) {
		this.ipatentManageDAO = ipatentManageDAO;
	}

	public void saveQxkw(String qxkwmc, String qxkwtype) {
		Qxkw qxkw = new Qxkw();
		qxkw.setId(qxkwtype);
		qxkw.setName(qxkwmc);
		this.getIpatentManageDAO().saveQxkw(qxkw);

	}

	public List getQxkw() {
		List qmList = new ArrayList();
		List qxkwtypeList = this.getIpatentManageDAO().findQxkwType("type");
		List<Qxkw> qxkwList = this.getIpatentManageDAO().findQxkw();
		Iterator it = qxkwList.iterator();
		while (it.hasNext()) {
			Qxkw qxkw = (Qxkw) it.next();
			QxkwModel qm = new QxkwModel();
			// qm.setId(qxkw.getId());
			Iterator tit = qxkwtypeList.iterator();
			while (tit.hasNext()) {
				Qxkw qk = (Qxkw) tit.next();

				if ("0000".equals(qxkw.getId())) {
					qm.setType("奖励办公室");
				}
				qm.setTypeid(qxkw.getId().substring(0, 2));
				if (qk.getId().equals(qm.getTypeid())) {

					qm.setType(qk.getName());
				}

			}
			qm.setId(qxkw.getId());
			qm.setName(qxkw.getName());
			qmList.add(qm);

		}
		return qmList;
	}

	public boolean verifyQxkw(QxkwModel qm) {
		List qxkwList = this.getIpatentManageDAO().findQxkwByName(
				qm.getName().trim());
		if (qxkwList.size() >= 1) {
			return true; // 用户已添加
		} else {
			return false;// 用户没有可已添加
		}

	}

	public QxkwModel getQxkwById(QxkwModel qm) {

		Qxkw qxkw = this.getIpatentManageDAO().findQxkwById(qm.getId());
		qm.setId(qxkw.getId());
		qm.setName(qxkw.getName());

		return qm;
	}

	public void updateQxkw(QxkwModel qm) {
		Qxkw qxkw = new Qxkw();
		qxkw.setId(qm.getId());
		qxkw.setName(qm.getName());
		this.getIpatentManageDAO().updateQxkw(qxkw);

	}

	public boolean deleteQxkw(String id) {
		List userList = this.getIpatentManageDAO().findUser(id);
		List basicList = this.getIpatentManageDAO().findBasicInfoByQxkw(id);
        System.out.println(userList.size()+"-"+basicList.size());
       
		if (userList.size() < 1 && basicList.size() < 1) {
			this.getIpatentManageDAO().deleteQxkw(id);
			return true;
		} else {
			return false;
		}

	}

	public void deleteQxkwType(String id) {

		this.ipatentManageDAO.deleteQxkwType(id);

	}

	public List getQxkwType() {

		List qxkwList = this.getIpatentManageDAO().findQxkwType("type");
		

		return qxkwList;

	}

	public void saveQxkwType(String qxkwmc) {
		List qxkwList = this.getIpatentManageDAO().findQxkwType("type");
		Qxkw qxkw = new Qxkw();
		Qxkw qk = (Qxkw) qxkwList.get(qxkwList.size() - 2);// 娶最后一个大类的id
		qxkw.setId(String.valueOf(Integer.parseInt(qk.getId()) + 10));
		qxkw.setName(qxkwmc);
		this.getIpatentManageDAO().saveQxkwType(qxkw);

	}

}
